package com.sjz.myfrist;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.Button;
import android.widget.Toast;

import com.sjz.myfrist.litepal.LitepalActivity;

import java.util.Locale;

/***
 *  首页
 */
public class MainActivity extends AppCompatActivity {

    //语音对象
    private TextToSpeech tts;
    private Button music_Video_id;
    private Button sensor_demo_two_id;
    private Button picture_page_id;
    private Button sensor_three_id;
    private Button sensor_four_id;
    private Button jsoup1_id;
    private Button db_crate_page_id;
    private Button webview_page_id;
    private Button httpclient_page_id;
    private Button okhttpclient_page_id;
    private Button xml_page_id;
    private Button xml_sax_page_id;
    private Button jsonobject_page_id;
    private Button gjson_page_id;
    private Button httputil_page_id;
    private Button exoplay_page_ids;
    private Button contacts_page_id;
    private Button callphone_page_id;
    private Button notification1_page_id;
    private Button db_crate_litepal_page_id;
    private Button act_send_page_id;
    private Button act_send_result1_page_id;
    private Button act_send_result2_page_id;
    private Button listview1_page_id;
    private Button listview2_page_id;
    private Button listview3_page_id;
    private Button recyclerview_page_id;
    private Button basic_button_page_id;
    private Button share_preference_page_id;
    private Button file_persistence_page_id;
    private Button sqlite_db_page_id;
    private Button sqlite_user_page_id;
    private Button repeat_click_page_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        music_Video_id=findViewById(R.id.music_Video_id);
        sensor_demo_two_id=findViewById(R.id.sensor_demo_two_id);
        picture_page_id= findViewById(R.id.picture_page_id);
        sensor_three_id= findViewById(R.id.sensor_three_id);
        sensor_four_id= findViewById(R.id.sensor_four_id);
        jsoup1_id= findViewById(R.id.jsoup1_id);
        db_crate_page_id= findViewById(R.id.db_crate_page_id);
        webview_page_id= findViewById(R.id.webview_page_id);
        httpclient_page_id= findViewById(R.id.httpclient_page_id);
        okhttpclient_page_id= findViewById(R.id.okhttpclient_page_id);
        xml_page_id= findViewById(R.id.xml_page_id);
        xml_sax_page_id= findViewById(R.id.xml_sax_page_id);
        jsonobject_page_id= findViewById(R.id.jsonobject_page_id);
        gjson_page_id=findViewById(R.id.gjson_page_id);
        httputil_page_id=findViewById(R.id.httputil_page_id);
        exoplay_page_ids=findViewById(R.id.exoplay_page_ids);
        contacts_page_id=findViewById(R.id.contacts_page_id);
        callphone_page_id=findViewById(R.id.callphone_page_id);
        notification1_page_id=findViewById(R.id.notification1_page_id);
        db_crate_litepal_page_id=findViewById(R.id.db_crate_litepal_page_id);
        act_send_page_id=findViewById(R.id.act_send_page_id);
        act_send_result1_page_id=findViewById(R.id.act_send_result1_page_id);
        act_send_result2_page_id=findViewById(R.id.act_send_result2_page_id);
        listview1_page_id=findViewById(R.id.listview1_page_id);
        listview2_page_id=findViewById(R.id.listview2_page_id);
        listview3_page_id=findViewById(R.id.listview3_page_id);
        recyclerview_page_id=findViewById(R.id.recyclerview_page_id);
        basic_button_page_id=findViewById(R.id.basic_button_page_id);
        share_preference_page_id=findViewById(R.id.share_preference_page_id);
        file_persistence_page_id=findViewById(R.id.file_persistence_page_id);
        sqlite_db_page_id=findViewById(R.id.sqlite_db_page_id);
        sqlite_user_page_id=findViewById(R.id.sqlite_user_page_id);
        repeat_click_page_id=findViewById(R.id.repeat_click_page_id);


        setListeners();//调用方法


        //语音播放初始化
        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == tts.SUCCESS) {
                    /*Toast.makeText(MainActivity.this,"成功输出语音",
                    Toast.LENGTH_SHORT).show();*/
                    // Locale loc1=new Locale("us");
                    // Locale loc2=new Locale("china");
                    // int result1 = tts.setLanguage(Locale.US); 手机比较特殊，只支持中文，不支持英文
                    int result1 = tts.isLanguageAvailable(Locale.US);
                    int result2 = tts.isLanguageAvailable(Locale.CHINA);
                    int result3 = 0;
                    int result4 = 0;
                    if (result1 == TextToSpeech.LANG_MISSING_DATA
                            || result1 == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(MainActivity.this, "语音引擎不支持英文",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        result3 = tts.setLanguage(Locale.US);
                    }
                    if (result2 == TextToSpeech.LANG_MISSING_DATA
                            || result2 == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(MainActivity.this, "语音引擎不支持中文",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        result4 = tts.setLanguage(Locale.CHINA);
                    }

                    if (result3 == TextToSpeech.LANG_MISSING_DATA
                            || result3 == TextToSpeech.LANG_NOT_SUPPORTED || result4 == TextToSpeech.LANG_MISSING_DATA
                            || result4 == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(MainActivity.this, "语音引擎初始化失败",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

    }

    //页面跳转 进口
    private void setListeners(){
        OnClick onclick=new OnClick();//实例化onclick
        music_Video_id.setOnClickListener(onclick);
        sensor_demo_two_id.setOnClickListener(onclick);
        picture_page_id.setOnClickListener(onclick);
        sensor_three_id.setOnClickListener(onclick);
        sensor_four_id.setOnClickListener(onclick);
        jsoup1_id.setOnClickListener(onclick);
        db_crate_page_id.setOnClickListener(onclick);
        webview_page_id.setOnClickListener(onclick);
        httpclient_page_id.setOnClickListener(onclick);
        okhttpclient_page_id.setOnClickListener(onclick);
        xml_page_id.setOnClickListener(onclick);
        xml_sax_page_id.setOnClickListener(onclick);
        jsonobject_page_id.setOnClickListener(onclick);
        gjson_page_id.setOnClickListener(onclick);
        httputil_page_id.setOnClickListener(onclick);
        exoplay_page_ids.setOnClickListener(onclick);
        contacts_page_id.setOnClickListener(onclick);
        callphone_page_id.setOnClickListener(onclick);
        notification1_page_id.setOnClickListener(onclick);
        db_crate_litepal_page_id.setOnClickListener(onclick);
        act_send_page_id.setOnClickListener(onclick);
        act_send_result1_page_id.setOnClickListener(onclick);
        act_send_result2_page_id.setOnClickListener(onclick);
        listview1_page_id.setOnClickListener(onclick);
        listview2_page_id.setOnClickListener(onclick);
        listview3_page_id.setOnClickListener(onclick);
        recyclerview_page_id.setOnClickListener(onclick);
        basic_button_page_id.setOnClickListener(onclick);
        share_preference_page_id.setOnClickListener(onclick);
        file_persistence_page_id.setOnClickListener(onclick);
        sqlite_db_page_id.setOnClickListener(onclick);
        sqlite_user_page_id.setOnClickListener(onclick);
        repeat_click_page_id.setOnClickListener(onclick);

    }


    private class OnClick implements  View.OnClickListener{
        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.music_Video_id://跳到音乐和视频跳转界面
                    Log.e("主方法跳转","music_Video_id");
                    Intent intent = null;
                    intent=new Intent(MainActivity.this, MusicAndVideoActivity.class);
                    startActivity(intent);
                    break;

                case R.id.sensor_demo_two_id://跳到传感器2界面
                    Log.e("主方法跳转","sensor_demo_two_id");
                    intent = null;
                    intent=new Intent(MainActivity.this, SensorActivity2.class);
                    startActivity(intent);
                    break;

                case R.id.picture_page_id://跳到图片界面
                    Log.e("主方法跳转","picture_page_id");
                    intent = null;
                    intent=new Intent(MainActivity.this, PictureActivity.class);
                    startActivity(intent);
                    break;

                case R.id.sensor_three_id://跳到测试传感器界面
                    intent = null;
                    intent=new Intent(MainActivity.this, SensorActivity3.class);
                    startActivity(intent);
                    break;
                case R.id.sensor_four_id://跳到测试传感器 计步器界面
                    intent = null;
                    intent=new Intent(MainActivity.this, SensorActivity4.class);
                    startActivity(intent);
                    break;

                case R.id.jsoup1_id://
                    intent = null;
                    intent=new Intent(MainActivity.this, JsoupActivity1.class);
                    startActivity(intent);
                    break;
                case R.id.db_crate_page_id:// sqlite 数据库
                    intent = null;
                    intent=new Intent(MainActivity.this, DbActivity.class);
                    startActivity(intent);
                    break;
                case R.id.db_crate_litepal_page_id://litepal数据库
                    intent = null;
                    intent=new Intent(MainActivity.this, LitepalActivity.class);
                    startActivity(intent);
                    break;
                case R.id.webview_page_id://webview
                    intent = null;
                    intent=new Intent(MainActivity.this, WebViewActivity.class);
                    startActivity(intent);
                    break;
                case R.id.httpclient_page_id://httpclient_page_id
                    intent = null;
                    intent=new Intent(MainActivity.this, HttpClientActivity.class);
                    startActivity(intent);
                    break;
                case R.id.okhttpclient_page_id://
                    intent = null;
                    intent=new Intent(MainActivity.this, OkHttpActivity.class);
                    startActivity(intent);
                    break;
                case R.id.xml_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, XMlActivity.class);
                    startActivity(intent);
                    break;
                case R.id.xml_sax_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, Xml2Activity.class);
                    startActivity(intent);
                    break;
                case R.id.jsonobject_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, JSONObjectActivity.class);
                    startActivity(intent);
                    break;
                case R.id.gjson_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, GJSONActivity.class);
                    startActivity(intent);
                    break;
                case R.id.httputil_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, HttpUtilActivity.class);
                    startActivity(intent);
                    break;
                case R.id.exoplay_page_ids:
                    intent = null;
                    intent=new Intent(MainActivity.this, ExoplayActivity.class);
                    startActivity(intent);
                    break;
                case R.id.contacts_page_id:
                        intent = null;
                    intent=new Intent(MainActivity.this, ContentResolverActivity.class);
                    startActivity(intent);
                    break;
                case R.id.callphone_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, CallPhonerActivity.class);
                    startActivity(intent);
                    break;
                case R.id.act_send_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, ActSendActivity.class);
                    startActivity(intent);
                    break;
                case R.id.act_send_result1_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, ActSendResultActivity.class);
                    startActivity(intent);
                    break;
                case R.id.act_send_result2_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, ActSendResultTwoActivity.class);
                    startActivity(intent);
                    break;
                case R.id.listview1_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, Csdn7.class);
                    startActivity(intent);
                    break;
                case R.id.listview2_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, ListView2.class);
                    startActivity(intent);
                    break;
                case R.id.listview3_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, ListView3.class);
                    startActivity(intent);
                    break;
                case R.id.recyclerview_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, BasicRecyclerView1.class);
                    startActivity(intent);
                    break;
                case R.id.basic_button_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, BasicButtonCase.class);
                    startActivity(intent);
                    break;
                case R.id.share_preference_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, SharePreferencesCase.class);
                    startActivity(intent);
                    break;
                case R.id.file_persistence_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, FilePersistenceTest.class);
                    startActivity(intent);
                    break;
                case R.id.sqlite_db_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, SqliteDb.class);
                    startActivity(intent);
                    break;
                case R.id.sqlite_user_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, SQLiteHelperActivity.class);
                    startActivity(intent);
                    break;
                case R.id.notification1_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, NotificationActivity.class);
                    startActivity(intent);
                    break;
                case R.id.repeat_click_page_id:
                    intent = null;
                    intent=new Intent(MainActivity.this, RepeatClick.class);
                    startActivity(intent);
                    break;

            }

        }
    }


    /**
     * 语音播报
     */
    @JavascriptInterface
    public void speechText(String text, String sd) {
        if (!sd.equals("")) {
            float sr = Float.parseFloat(sd);
            tts.setSpeechRate(sr);
        }
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void Welcome(View view) {
        speechText("陈龙兄,好久不见", "1");
        Toast.makeText(this, "陈龙兄,好久不见", Toast.LENGTH_SHORT).show();
    }

    /***
     * 传感器 类型查询页面跳转
     * @param view
     */
    public void changepage(View view) {
        Intent intent = null;
        intent = new Intent(MainActivity.this, SensorActivity.class);
        startActivity(intent);
    }
}